package com.api.shopapi.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.api.shopapi.entity.GoodsClass;
import com.api.shopapi.entity.GoodsClassGovernment;
import com.api.shopapi.entity.dto.GoodsClassGovernmentDTO;
import com.api.shopapi.mapper.GoodsClassGovernmentMapper;
import com.api.shopapi.mapper.GoodsClassMapper;
import com.api.shopapi.service.GoodsClassGovernmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service("GoodsClassGovernmentServiceImplOne")
public class GoodsClassGovernmentServiceImpl implements GoodsClassGovernmentService {


    @Autowired
    private GoodsClassGovernmentMapper goodsClassGovernmentMapper;

    @Autowired
    private GoodsClassMapper goodsClassMapper;

    @Override
    @Transactional
    public int insert(String governmentCode, JSONArray goodsClassCodeList) {

        //删除政府下所有分类
        List<GoodsClassGovernment> goodsClassGovernmentList = goodsClassGovernmentMapper.getListByGovernmentCode(governmentCode);
        for (GoodsClassGovernment goodsClassGovernment : goodsClassGovernmentList) {
            goodsClassGovernmentMapper.delete(goodsClassGovernment.getGoodsClassGovernmentCode());
        }

        for (int i=0;i<goodsClassCodeList.size();i++){
            String goodsClassCode = goodsClassCodeList.get(i).toString();
            GoodsClass goodsClass = goodsClassMapper.getByGoodsClassCode(goodsClassCode);
            GoodsClassGovernment goodsClassGovernment1=new GoodsClassGovernment();
            goodsClassGovernment1.setGoodsClassGovernmentCode(UUID.randomUUID().toString());
            goodsClassGovernment1.setGoodsClassCode(goodsClass.getGoodsClassCode());
            goodsClassGovernment1.setParentGoodsClassCode(goodsClass.getParentGoodsClassCode());
            goodsClassGovernment1.setIcon(goodsClass.getIcon());
            goodsClassGovernment1.setName(goodsClass.getName());
            goodsClassGovernment1.setStatus(goodsClass.getStatus());
            goodsClassGovernment1.setSort(goodsClass.getSort());
            goodsClassGovernment1.setIsGroup(goodsClass.getIsGroup());
            goodsClassGovernment1.setParentGoodsClassGovernmentCode(goodsClass.getParentGoodsClassCode());
            goodsClassGovernment1.setGovernmentCode(governmentCode);
            goodsClassGovernmentMapper.insert(goodsClassGovernment1);
        }
        return 1;
    }

    @Override
    public int update(GoodsClassGovernment goodsClassGovernment) {
        return goodsClassGovernmentMapper.update(goodsClassGovernment);
    }

    @Override
    public GoodsClassGovernment getByGoodsClassGovernmentCode(String goodsClassGovernmentCode) {
        return goodsClassGovernmentMapper.getByGoodsClassGovernmentCode(goodsClassGovernmentCode);
    }

    @Override
    public GoodsClassGovernment getByName(String name,String governmentCode) {
        return goodsClassGovernmentMapper.getByName(name,governmentCode);
    }

    @Override
    public List<GoodsClassGovernment> getListByGovernmentCode(String governmentCode) {
        return goodsClassGovernmentMapper.getListByGovernmentCode(governmentCode);
    }

    @Override
    public List<GoodsClassGovernmentDTO> getAllListByParentGoodsClassCode(String governmentCode, String parentGoodsClassCode) {
        return goodsClassGovernmentMapper.getAllListByParentGoodsClassCode(governmentCode,parentGoodsClassCode);
    }

    @Override
    public List<GoodsClassGovernmentDTO> getListByParentGoodsClassCode(String governmentCode, String ParentGoodsClassCode) {
        return goodsClassGovernmentMapper.getListByParentGoodsClassCode(governmentCode,ParentGoodsClassCode);
    }

    @Override
    public PageInfo<GoodsClassGovernment> getPageList(int pageIndex, int pageSize, String governmentCode, String name, int status) {
        PageHelper.startPage(pageIndex,pageSize);
        List<GoodsClassGovernment> goodsClassGovernmentList = goodsClassGovernmentMapper.getPageList(governmentCode, name, status);
        PageInfo<GoodsClassGovernment> goodsClassGovernmentPageInfo=new PageInfo<>(goodsClassGovernmentList);
        return goodsClassGovernmentPageInfo;
    }

    @Override
    public PageInfo<GoodsClassGovernmentDTO> getPageListByParentGoodsClassCode(int pageIndex, int pageSize, String governmentCode, String name, String parentGoodsClassCode) {
        PageHelper.startPage(pageIndex,pageSize);
        List<GoodsClassGovernmentDTO> goodsClassGovernmentDTOList = goodsClassGovernmentMapper.getPageListByParentGoodsClassCode(governmentCode, name, parentGoodsClassCode);
        return new PageInfo<>(goodsClassGovernmentDTOList);
    }

    @Override
    public List<GoodsClassGovernment> getSecondClassList(String governmentCode,String parentGoodsClassCode) {
        return goodsClassGovernmentMapper.getSecondClassList(governmentCode,parentGoodsClassCode);
    }

    @Override
    public List<GoodsClassGovernment> getFirstClassList(String governmentCode, String parentGoodsClassCode) {
        return goodsClassGovernmentMapper.getFirstClassList(governmentCode,parentGoodsClassCode);
    }
}
